<?php
namespace Goods;
class DetailController extends BaseController{
	public function index(){
		global $G,$lang;
		$id = intval($_GET['id']);
		$goods = goods_get_data(array('id'=>$id));
		$description = goods_get_desc($id);
		$piclist = image_get_list($id, 'goods');
		
		$pathlist = category_get_parents($goods['catid']);
		rsort($pathlist);
		
		$G['title'] = $goods['name'];
		include template('detail');
	}
	
	/**
	 * 显示商品评论
	 */
	public function showcomment(){
		global $G,$lang;
		$dataid = intval($_GET['dataid']);
		$pagesize  = 20;
		$condition = array('dataid'=>$dataid, 'datatype'=>'goods');
		$totalnum  = comment_get_num($condition);
		$pagecount = $totalnum < $pagesize ? 1 : ceil($totalnum/$pagesize);
		$commentlist = comment_get_page($condition, $G['page'], $pagesize);
		$pages = $this->showPages($G['page'], $pagecount, $totalnum, "dataid=$dataid");
		
		if ($commentlist) {
			$uids = $comma = '';
			foreach ($commentlist as $list){
				$uids.= $comma.$list['uid'];
				$comma = ',';
			}
			
			if ($uids) {
				$userlist = member_get_list(array('uid'=>array('IN', $uids)), $pagesize);
			}
			unset($uids,$comma,$list);
		}
		
		include template('goods_comment');
	}

	/**
	 * 添加商品到收藏夹
	 */
	public function favorite(){
		global $G;
		
		$id = intval($_GET['id']);
		$goods = goods_get_data(array('id'=>$id));
		$image = image_get_data(array('dataid'=>$id,'datatype'=>'goods'));
		
		if (!favorite_get_num(array('dataid'=>$id,'datatype'=>'goods'))){
			favorite_add_data(array(
					'uid'=>$this->uid,
					'dataid'=>$id,
					'datatype'=>'goods',
					'title'=>$goods['name'],
					'image'=>$image['image'],
					'dateline'=>TIMESTAMP
			));
		}
		$this->showAjaxReturn(array('result'=>'SUCCESS'));
	}
}